package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.ShiftPermissionBean;
import com.attendance.www.hr.beans.ShiftPermissionPageBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IShiftPermissionService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 班次权限
 * @author 崔棋
 * 2020-06-09
 */
@Controller
@RequestMapping("/shiftPermission")
public class ShiftPermissionController {
	
	@Resource
	private IShiftPermissionService shiftPermissionService;

	/**
	 * 获取班次权限接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getShiftPermissionList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getShiftPermissionList(HttpServletRequest request,@RequestBody ShiftPermissionBean shiftPermissionBean) {
		Integer pageNo = shiftPermissionBean.getPageNo();
		Integer pageCount = shiftPermissionBean.getPageCount();

		if (pageNo ==null||pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		ShiftPermissionPageBean shiftPermissionPageBean = new ShiftPermissionPageBean();
		shiftPermissionPageBean = shiftPermissionService.getShiftPermissionList(shiftPermissionBean, startNum, pageCount);
		return ResultMap.success(shiftPermissionPageBean);
	}
	
	/**
	 * 添加班次权限接口			
	 * @param request
	 * @param ShiftPermissionBean
	 * @return
	 */
	@RequestMapping(value = "/saveShiftPermission.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveShiftPermission(HttpServletRequest request, @RequestBody ShiftPermissionBean shiftPermissionBean) {
		if (shiftPermissionBean != null) {
			if (StringUtils.isBlank(shiftPermissionBean.getShiftName())) {
				throw new ResultFaultException("0_00001");
			}
			if (shiftPermissionService.getVerifyShiftPermission(shiftPermissionBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (shiftPermissionService.saveShiftPermission(shiftPermissionBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	
	/**
	 * 修改班次权限接口
	 * @param request
	 * @param ShiftPermissionBean
	 * @return
	 */
	@RequestMapping(value = "/updateShiftPermission.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateShiftPermission(HttpServletRequest request, @RequestBody ShiftPermissionBean shiftPermissionBean) {
		if (shiftPermissionBean != null) {
			if (StringUtils.isBlank(shiftPermissionBean.getShiftName()) 
					|| shiftPermissionBean.getShiftId()==null) {
				throw new ResultFaultException("0_00001");
			}
			if (shiftPermissionService.getVerifyShiftPermission(shiftPermissionBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (shiftPermissionService.updateShiftPermission(shiftPermissionBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	
	/**
	 * 删除班次权限接口		
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteShiftPermission.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteShiftPermission(HttpServletRequest request) {
		String shiftId = request.getParameter("shiftId");
		if (StringUtils.isBlank(shiftId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(shiftPermissionService.deleteShiftPermission(shiftId));
	}
}
